package com.rock.upms.web.upms.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rock.common.base.BaseController;
import com.rock.common.base.BaseQuery;
import com.rock.common.base.BaseResult;
import com.rock.common.util.StringUtil;
import com.rock.upms.api.model.service.IUpmsPersonService;
import com.rock.upms.model.UpmsPerson;
import com.rock.upms.model.UpmsPersonExample;
import com.rock.upms.model.UpmsPersonExample.Criteria;
import com.rock.upms.web.common.UpmsWebConfig;

@Controller
public class PersonController extends BaseController {

	private static Logger log = LoggerFactory.getLogger(PersonController.class);
	@Autowired
	private IUpmsPersonService upmsPersonService;

	/**
	 * 功能：测试<br>
	 * 说明：点击菜单或链接进行列表初始化，初始化默认查询条件等<br>
	 * 请求方式：链接（GET）
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return 初始化参数
	 */
	@ResponseBody
	@RequestMapping(value = "upms/person/getPerson", method = RequestMethod.GET)
	public BaseResult toList(HttpServletRequest req,String usercode) {
		UpmsPersonExample example=new UpmsPersonExample();
		Criteria criteria=example.createCriteria();
		criteria.andUsercodeEqualTo(usercode);
		List<UpmsPerson> personList=upmsPersonService.selectByExample(example);
		return BaseResult.success("").add("personList", personList);
	}

	/**
	 * 功能： 列表初始化<br>
	 * 说明：点击菜单或链接进行列表初始化，初始化默认查询条件等<br>
	 * 请求方式：链接（GET）
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return 初始化参数
	 */
	@ResponseBody
	@RequestMapping(value = "upms/person/list", method = RequestMethod.GET)
	public BaseResult toList(Model model) {
		BaseQuery query = new BaseQuery(1, UpmsWebConfig.getPageSize(), true);// 构造函数按默认分页方式分页，从第一页开始
		query.add("isDelete", "0");
		return BaseResult.success("").add("queryArg", query);
	}

	/**
	 * 功能：列表查询<br>
	 * 说明：<br>
	 * （1）列表初始化完成后显示初始化数据<br>
	 * （2）页面查询条件发生改变后的查询<br>
	 * 请求方式：ajax post请求
	 * 
	 * @param query
	 * @param request
	 * @param response
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequestMapping(value = "upms/person/list", method = RequestMethod.POST)
	public BaseResult list(BaseQuery query) {
		int pageNum = query.getPageNum();// 页号
		int dataNum = query.getPageSize();// 每页显示记录数
		int navigatePages = UpmsWebConfig.getPageNavigatePages();// 导航显示页数
		// 封装查询条件（自动生成三个，请根据实际修改）
		String isDelete = (String) query.getExtend().get("isDeleted");// 是否删除
		String usercode = (String) query.getExtend().get("usercode");// 机构号
		String username = (String) query.getExtend().get("username");// 机构名模糊匹配
		String orderBy = " id asc ";// 默认ID升序
		UpmsPersonExample personExample = new UpmsPersonExample();
		Criteria criteria = personExample.or();
		if (StringUtil.isNotBlank(isDelete)) {
			criteria.andIsDeletedEqualTo(isDelete);
		}
		if (StringUtil.isNotBlank(usercode)) {
			criteria.andUsercodeEqualTo(usercode);
		}
		if (StringUtil.isNotBlank(username)) {
			criteria.andUsernameLike("%" + username + "%");
		}
		personExample.setOrderByClause(orderBy);
		if (query.getIsPagination()) {
			PageHelper.startPage(pageNum, dataNum);
		} else {
			// 不分页查询
			PageHelper.startPage(pageNum, 0);
		}
		List<UpmsPerson> deptList = upmsPersonService.selectByExample(personExample);
		PageInfo<UpmsPerson> page = new PageInfo<UpmsPerson>(deptList, navigatePages);
		return BaseResult.success("").add("pageInfo", page);
	}

	/**
	 * 功能：新增（初始化）<br>
	 * 请求方式：ajax get
	 * 
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequestMapping(value = "upms/person/add", method = RequestMethod.GET)
	public BaseResult toAdd() {
		UpmsPerson vo = new UpmsPerson();
		vo.setCreateUsercode("");
		vo.setCreateUsername("");
		vo.setCreateTime(new Date());
		return BaseResult.success("").add("upmsPerson", vo);
	}

	/**
	 * 功能：新增（保存）<br>
	 * 请求方式：ajax
	 * 
	 * @param vo
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequestMapping(value = "upms/person/add", method = RequestMethod.POST)
	public BaseResult add(UpmsPerson vo) {
		log.trace("--->add vo:" + vo.toString());
		vo.setCreateUsercode("");
		vo.setCreateUsername("");
		vo.setCreateTime(new Date());
		vo.setIsDeleted("0");
		int result = upmsPersonService.insert(vo);
		if (result == 0) {
			return BaseResult.fail("保存失败");
		} else {
			return BaseResult.success("保存成功");
		}
	}

	/**
	 * 功能：删除 <br>
	 * 说明：根据主键ID删除记录，ID未加密
	 * 
	 * @param cryptId
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequestMapping(value = "upms/person/delete/{id}", method = RequestMethod.GET)
	public BaseResult delete(@PathVariable("id") String id) {
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return BaseResult.fail("你操作的数据不存在");
		} else {
			int backint = upmsPersonService.deleteByPrimaryKey(idint);
			if (backint != 0) {
				return BaseResult.success("删除成功");
			} else {
				return BaseResult.fail("删除失败");
			}
		}
	}

	/**
	 * 功能：修改（初始化）<br>
	 * 说明：根据主键获取记录，主键未加密<br>
	 * 请求方式：ajax
	 * 
	 * @param id
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequestMapping(value = "upms/person/edit/{id}", method = RequestMethod.GET)
	public BaseResult toEdit(@PathVariable("id") String id) {
		UpmsPerson vo = null;
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return BaseResult.fail("你操作的数据不存在");
		} else {
			vo = (UpmsPerson) upmsPersonService.selectByPrimaryKey(idint);
		}
		return BaseResult.success("").add("upmsPerson", vo);
	}

	/**
	 * 功能：修改（保存）<br>
	 * 请求方式：ajax
	 * 
	 * @param vo
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequestMapping(value = "upms/person/edit", method = RequestMethod.POST)
	public BaseResult edit(UpmsPerson vo) {
		log.trace("--->edit vo:" + vo.toString());
		int result = upmsPersonService.insertSelective(vo);
		if (result == 0) {
			return BaseResult.fail("保存失败");
		} else {
			return BaseResult.success("保存成功");
		}
	}

	/**
	 * 功能：查看<br>
	 * 说明：根据主键获取记录，主键未加密<br>
	 * 请求方式：ajax
	 * 
	 * @param id
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequestMapping(value = "upms/person/read/{id}", method = RequestMethod.GET)
	public BaseResult read(@PathVariable("id") String id) {
		UpmsPerson vo = null;
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return BaseResult.fail("你操作的数据不存在");
		} else {
			vo = (UpmsPerson) upmsPersonService.selectByPrimaryKey(idint);
		}
		return BaseResult.success("").add("upmsPerson", vo);
	}
}